﻿using System.Web.Mvc;

namespace dPet.Web.Admin.Controllers
{
	using dPet.Application.DataObjects;
	using dPet.Application.Services;
	using dPet.Web.Admin.Fliters;
	using dPet.Web.Admin.ViewModels;
	using Hangerd.Mvc;

	public class ManageController : HangerdController
	{
		private readonly IBasicDataService _basicDataService;
		private readonly IAccountService _accountService;

		public ManageController(
			IBasicDataService basicDataService,
			IAccountService accountService)
		{
			_basicDataService = basicDataService;
			_accountService = accountService;
		}

		#region Hospital

		[AdminLoginAuth]
		public ActionResult HospitalList()
		{
			var hospitals = _basicDataService.GetHospitals();

			return View(hospitals);
		}

		[AdminLoginAuth]
		public ActionResult Hospital(string id)
		{
			var hospital = !string.IsNullOrWhiteSpace(id)
				? _basicDataService.GetHospital(id)
				: new HospitalDto();

			if (hospital == null)
			{
				return RedirectToAction("HospitalList");
			}

			return View(hospital);
		}

		[HttpPost]
		[AdminLoginAuth]
		public ActionResult SaveHospital(HospitalDto hospitalDto)
		{
			var result = !string.IsNullOrWhiteSpace(hospitalDto.Id)
				? _basicDataService.UpdateHospital(hospitalDto.Id, hospitalDto, AdministratorModel.Current)
				: _basicDataService.AddHospital(hospitalDto, AdministratorModel.Current);

			return base.AlertAndRedirect(result.Message, "/Manage/HospitalList");
		}

		#endregion

		#region Staff Role

		[AdminLoginAuth]
		public ActionResult StaffRoleList()
		{
			var roles = _basicDataService.GetStaffRoles();

			return View(roles);
		}

		[AdminLoginAuth]
		public ActionResult StaffRole(string id)
		{
			var role = !string.IsNullOrWhiteSpace(id)
				? _basicDataService.GetStaffRole(id)
				: new StaffRoleDto();

			if (role == null)
			{
				return RedirectToAction("StaffRoleList");
			}

			return View(role);
		}

		[HttpPost]
		[AdminLoginAuth]
		public ActionResult SaveStaffRole(StaffRoleDto staffRoleDto)
		{
			var result = !string.IsNullOrWhiteSpace(staffRoleDto.Id)
				? _basicDataService.UpdateStaffRole(staffRoleDto.Id, staffRoleDto, AdministratorModel.Current)
				: _basicDataService.AddStaffRole(staffRoleDto, AdministratorModel.Current);

			return base.AlertAndRedirect(result.Message, "/Manage/StaffRoleList");
		}

		#endregion

		#region Staff

		[AdminLoginAuth]
		public ActionResult StaffList(string hospitalId)
		{
			var hospital = _basicDataService.GetHospital(hospitalId);

			if (hospital == null)
			{
				return RedirectToAction("HospitalList");
			}

			return View(new StaffListModel
			{
				Hospital = hospital,
				Staffs = _accountService.GetStaffs(hospitalId),
				StaffRoles = _basicDataService.GetStaffRoles()
			});
		}

		[AdminLoginAuth]
		public ActionResult Staff(string id)
		{
			var staff = _accountService.GetStaff(id);

			if (staff == null)
			{
				return RedirectToAction("HospitalList");
			}

			return View(new StaffModel
			{
				Staff = staff,
				StaffRoles = _basicDataService.GetStaffRoles()
			});
		}

		[HttpPost]
		[AdminLoginAuth]
		public ActionResult SaveStaff(StaffDto staffDto)
		{
			var result = !string.IsNullOrWhiteSpace(staffDto.Id)
				? _accountService.UpdateStaff(staffDto.Id, staffDto, AdministratorModel.Current)
				: _accountService.AddStaff(staffDto, AdministratorModel.Current);

			return base.AlertAndRedirect(result.Message, string.Format("/Manage/StaffList?hospitalId={0}", staffDto.HospitalId));
		}

		[HttpPost]
		[AdminLoginAuth(Ajax = true, DefaultAjaxResult = "{\"Success\":false,\"Message\":\"操作失败\"}")]
		public ContentResult ResetPassword(string staffId, string newPassword)
		{
			var result = _accountService.ResetStaffPassword(staffId, newPassword, AdministratorModel.Current);

			return JsonContent(new { Success = result.Value, Message = result.Message });
		}

		#endregion
	}
}
